<?php
namespace Manage\Controller;
use Think\Controller;

/**
 * Class AdminPumpGateController
 * @package Manage\Controller
 * 泵站闸门
 */
class AdminPumpGateController extends Controller {

    private $model_name = 'PumpGate';
    private $number_field = 'gate_id';

    public function index(){
        $this->display();
    }

    public function get_all(){
        $list = get_date_grid($this->model_name);
        echo json_encode($list);
    }

    public function add(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            $one = D($this->model_name)->queryObj($data[$this->number_field], $this->number_field);
            if($one){
                echo json_encode(array('status'=>-1,'msg'=>'编号已经存在，请重新输入！'));
                exit;
            }
            $one = $model->addObj($data, 'id');
            if(!$one){
                echo json_encode(array('status'=>-1,'msg'=>'操作失败！'));
                exit;
            }else{
                echo json_encode(array('status'=>1,'msg'=>'保存成功！'));
            }
            return;
        }
    }

    public function edit(){
        $model = D($this->model_name);
        if(IS_POST){
            $post_data = filterInput($_POST);
            $one = $model->save($post_data);
            if(!$one){
                echo json_encode(array('status'=>-1,'msg'=>'操作失败！'));
                exit;
            }else{
                echo json_encode(array('status'=>1,'msg'=>'保存成功！'));
            }
            return;
        }
    }

    public function deleteArray(){
        $data = filterInput($_POST);
        $ids = $data['ids'];
        $model = D($this->model_name);
        $item = $model->deleteArray($ids);
        if($item){
            echo json_encode(array('status'=>1));
        }else{
            echo json_encode(array('status'=>0));
        }
    }

    public function get_obj_json(){
        $id = I('get.id');
        $obj = D($this->model_name)->queryObj($id, 'id');
        echo $obj?json_encode($obj):json_encode(array('status'=> -1));
    }

    public function just_see_fy(){
        $id = I('get.id');
        $fy = D($this->model_name)->queryObj($id, 'id');
        $this->assign('fy', $fy);
        $this->display();
    }

    public function import(){
        header("Content-type: text/html; charset=utf-8");
        $filename = '.'.I('get.file_path');
        $start_row = 2;
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($filename);
        $php_excel = $php_reader->load($filename);

        $station_model = D('PumpingStation');
        $model = D('PumpGate');
        $p_model = D('PumpMachine');
        for($sheet_index=0;$sheet_index<1;$sheet_index++){
            $currentSheet = $php_excel->getSheet($sheet_index);
            $allRow = $currentSheet->getHighestRow();
            for($currentRow = $start_row;$currentRow <= $allRow;$currentRow++){
                $map['ps_name'] = array('eq', $currentSheet->getCell('A'.$currentRow)->getValue());
                $one = $station_model->where($map)->find();
                if(!$one){
                    $station_data = array();
                    $station_data['ps_name'] = $currentSheet->getCell('A'.$currentRow)->getValue();
                    $station_data['ps_catg1'] = 0;
                    $station_data['ps_catg2'] = 0;
                    $station_data['ps_catg3'] = 0;
                    $one = $station_model->add($station_data);
                }else{
                    $one = $one['id'];
                }
                $p_one = $p_model->queryObj($currentSheet->getCell('B'.$currentRow)->getValue(), 'scrmach_number');
                $fy_data = array();
                $fy_data['ps_id'] = $one;
                $fy_data['scrmach_id'] = $p_one?$p_one['id']:0;
                $fy_data['gate_id'] = $currentSheet->getCell('C'.$currentRow)->getValue();
                $fy_data['gate_name'] = $currentSheet->getCell('D'.$currentRow)->getValue();
                $fy_data['gate_typ'] = $currentSheet->getCell('E'.$currentRow)->getValue();
                $fy_data['gate_mfr'] = $currentSheet->getCell('F'.$currentRow)->getValue();
                $fy_data['gate_model'] = $currentSheet->getCell('G'.$currentRow)->getValue();
                $fy_data['gate_motor_power'] = $currentSheet->getCell('H'.$currentRow)->getValue();
                $fy_data['gata_elev'] = $currentSheet->getCell('I'.$currentRow)->getValue();
                $fy_data['gate_h'] = $currentSheet->getCell('J'.$currentRow)->getValue();
                $fy_data['gate_wid'] = $currentSheet->getCell('K'.$currentRow)->getValue();
                $fy_data['gate_cntl_typ'] = $currentSheet->getCell('L'.$currentRow)->getValue();
                $fy_data['gate_swit_sty'] = $currentSheet->getCell('M'.$currentRow)->getValue();
                $fy_data['gate_row_num'] = $currentSheet->getCell('N'.$currentRow)->getValue();
                $fy_data['gate_hole_num'] = $currentSheet->getCell('O'.$currentRow)->getValue();
                $fy_data['gate_svc_dt'] = $currentSheet->getCell('P'.$currentRow)->getValue();
                $model->add($fy_data);
            }
        }
        echo '导入完成';
    }

    public function export(){
        header("Content-type: text/html; charset=utf-8");
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        $temp_file = './exportexcel/gate.xls';
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($temp_file);
        if(!$php_reader->canRead($filename)){
            echo '无法识别的Excel文件！';
            return false;
        }
        $php_excel = $php_reader->load($filename);
        $currentSheet = $php_excel->getSheet(0);
        $station_model = D('PumpingStation');
        $model = D('PumpGate');
        $p_model = D('PumpMachine');
        $list = $model->getAll();
        $currentRow = 2;
        foreach($list as $fy){
            $station = $station_model->queryObj($fy['ps_id'], 'id');
            if(!$station){
                continue;
            }
            $p_one = $p_model->queryObj($fy['scrmach_id'], 'id');
            $currentSheet->setCellValue('A'.$currentRow, $station['ps_name']);
            $currentSheet->setCellValue('B'.$currentRow, $p_one?$p_one['scrmach_number']:'');
            $currentSheet->setCellValue('C'.$currentRow, $fy['gate_id']);
            $currentSheet->setCellValue('D'.$currentRow, $fy['gate_name']);
            $currentSheet->setCellValue('E'.$currentRow, gate_typ($fy['gate_typ'], false));
            $currentSheet->setCellValue('F'.$currentRow, $fy['gate_mfr']);
            $currentSheet->setCellValue('G'.$currentRow, $fy['gate_model']);
            $currentSheet->setCellValue('H'.$currentRow, $fy['gate_motor_power']);
            $currentSheet->setCellValue('I'.$currentRow, $fy['gata_elev']);
            $currentSheet->setCellValue('J'.$currentRow, $fy['gate_h']);
            $currentSheet->setCellValue('K'.$currentRow, $fy['gate_wid']);
            $currentSheet->setCellValue('L'.$currentRow, gate_cont_type_option($fy['gate_cntl_typ'], false));
            $currentSheet->setCellValue('M'.$currentRow, gate_swit_sty_option($fy['gate_swit_sty'], false));
            $currentSheet->setCellValue('N'.$currentRow, $fy['gate_row_num']);
            $currentSheet->setCellValue('O'.$currentRow, $fy['gate_hole_num']);
            $currentSheet->setCellValue('P'.$currentRow, $fy['gate_svc_dt']);
            $currentRow++;
        }
        $currentSheet->getStyle('A1:P'.$currentRow)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $currentSheet->getStyle('A1:P'.$currentRow)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $currentSheet->getStyle('A1:P'.$currentRow)->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        $php_writer = new \PHPExcel_Writer_Excel5($php_excel);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=闸门.xls");
        header('Cache-Control: max-age=0');
        $php_writer->save('php://output');
    }
}